import http.cookiejar
import urllib.request
from urllib.parse import urlparse
from urllib.parse import ParseResult
from bs4 import BeautifulSoup
import  requests
#登录页的地址
url = "http://120.236.178.42:8888/cars/"
#登录的请求头部
header = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
}

#构建请求
request = urllib.request.Request(url = url,headers = header)

#创建cookie容器，重写URLlib的opener
cookie = http.cookiejar.MozillaCookieJar("cars.txt") #创建一个将cookie保存到本地文件的cookie实例
#创建一个URLlib2的容器来加载cookie
handler = urllib.request.HTTPCookieProcessor(cookie)


#创建一个可以保存cookie的opener
opener = urllib.request.build_opener(handler)

#用当前opener打开一个请求，会自动将响应的cookie记录到我们指定的文件当中

response = opener.open(request)

data = {
    "userName":"anlianbaoxian",
    "password":"123456",
    "Submit":""

}

sendData = urllib.parse.urlencode(data).encode(encoding='UTF8') #封装请求数据

login_url = "http://120.236.178.42:8888/cars/Login_login.do"

header["Referer"] = "http://120.236.178.42:8888/cars/login.jsp"

login_request = urllib.request.Request(url = login_url,data = sendData,headers = header)

#携带cookie进行请求
response = opener.open(login_request)

cookie.save(ignore_expires = True,ignore_discard = True)

#body = response.read().decode('utf-8')

"""
header=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')]
#创建opener对象
opener1=urllib.request.build_opener()
opener1.addheaders=header
#设置opener对象作为urlopen()使用的全局opener
urllib.request.install_opener(opener1)
"""
# 创建MozillaCookieJar实例对象
cookie1 = http.cookiejar.MozillaCookieJar()
# 从文件中读取cookie内容到变量
cookie1.load('cars.txt', ignore_discard=True, ignore_expires=True)

#parame={}
#parame['id']='31382'
#url_parame=urllib.parse.urlencode(parame).encode('utf-8')

id='31382'
#对keyword进行URL编码
#keyword=urllib.request.quote(id)
#print(keyword)
dataUrl = "http://120.236.178.42:8888/cars/Contract_contractDetail.do"+"?"+"id=31382"

#urlParseResult = ParseResult(scheme='http', netloc='120.236.178.42:8888', path='/cars/Contract_contractDetail.do', params='', query='id=31382', fragment='ff')


dataParame=urlparse(dataUrl)
req = urllib.request.Request(dataUrl)
req.add_header('cookie','JSESSIONID=6A2A7206E0A9615BB39F8914B6F51407')
data = bytes(urllib.parse.urlencode({'id': '31382'}), encoding= 'utf8')
req.method='GET'
print(req.full_url)
response1=urllib.request.urlopen(req,data=data)
html=response1.read().decode("utf-8")

response1.close()

print(html)


"""
opener1 = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
response = urllib.request.urlopen(dataUrl)
url = urllib.parse.urlunparse(urlParseResult)

print(url)
response1=opener1.open(url)
buff = response1.read()
html = buff.decode("utf8")
response1.close()
print(html)
"""

"""
payload={'id':'31340'}
headers = {
    'content-type': 'application/json',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0',
    'Host':'120.236.178.42:8888',
    'Upgrade-Insecure-Requests':'1',
    'Connection':'keep-alive',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Cookie':'JSESSIONID=6A2A7206E0A9615BB39F8914B6F51407'
           }
response2=requests.get("http://120.236.178.42:8888/cars/Contract_contractDetail.do",params=payload,headers=headers,allow_redirects=False,fragment='ff')
print(urlparse(response2.url));
print(response2.text)
print(response2.history)
print(response2.status_code)
"""
